﻿using ITeBooks.API.Library.Model;
using ITeBooks.API.Library.Converter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MakarovDev.ExpandCollapsePanel;

namespace IteBooks.API.Library.WindowsFormSample
{
    public partial class frmFindBook : Form
    {
        private EBookQuery query;
        private QueryResult queryResult;
        private int actualPage = 1;
        private const string statusTemplate = "{0} result(s) found to the search parameter(s): \"{1}\".";
        private const string resultTemplate = "{0} result(s) found.";
        public frmFindBook()
        {
            InitializeComponent();
            this.dataGridView1.AutoGenerateColumns = false;

            this.btnExecute.Image = (Image)(IteBooks.API.Library.WindowsFormSample.Properties.Resources.exec);
            this.Icon = (Icon)(IteBooks.API.Library.WindowsFormSample.Properties.Resources.favicon);

            this.stLabelChar.Text = String.Empty;
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            this.actualPage = 1;
            this.query = new EBookQuery(this.txtQuery.Text, this.chkLoadImages.Checked);
            this.Find(this.query);
            this.tbcResult.SelectedIndex = 0;
            if (this.queryResult.Total > 0)
            {
                string message;
                string status = String.Format(statusTemplate, this.queryResult.Total,this.query.Query);

                message = String.Format(resultTemplate, queryResult.Total);

                MessageBox.Show(message, "Query result", MessageBoxButtons.OK, MessageBoxIcon.Information);

                this.stLabelResult.Text = status;
                this.stLabelResult.ForeColor = Color.Blue;
            }
            else
            {
                MessageBox.Show("No result found", "Query result", MessageBoxButtons.OK, MessageBoxIcon.Information);
                string status = String.Format(statusTemplate, this.queryResult.Total, this.query.Query);
                this.stLabelResult.Text = status;
                this.stLabelResult.ForeColor = Color.Red;
            }

        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            this.ShowEbookDetails(this.dataGridView1.SelectedRows);
        }

        private void ShowEbookDetails(DataGridViewSelectedRowCollection selectedRows)
        {
            EBookQuery query;
            JSONEBookConverter converter;
            foreach (DataGridViewRow row in selectedRows)
            {
                EBook ebook = row.DataBoundItem as EBook;

                query = new EBookQuery(ebook.ID, true);
                converter = new JSONEBookConverter(query);

                QueryResult result = converter.FindByID();
                this.tbpDetails.Controls.Add(this.GeneretaPanel(result.EBook));
            }

            this.tbcResult.SelectedIndex = 1;
        }

        private ExpandCollapsePanel GeneretaPanel(EBook ebook)
        {
            ExpandCollapsePanel expandCollapsePanel1 = new MakarovDev.ExpandCollapsePanel.ExpandCollapsePanel();
            PictureBox pctBox = new PictureBox();
            TextBox txtDescription = new TextBox();
            TextBox txtISBN = new TextBox();
            Label lblDescription = new Label();
            Label lblISBN = new Label();
            TextBox txtAuthors = new TextBox();
            Label lblAuthors = new Label();
            TextBox txtSubtitle = new TextBox();
            Label lblSubtitle = new Label();
            TextBox txtID = new TextBox();
            Label lblID = new Label();
            TextBox txtTitle = new TextBox();
            Label lblTitle = new Label();
            TextBox txtYear = new TextBox();
            Label lblYear = new Label();
            TextBox txtPublisher = new TextBox();
            TextBox txtPages = new TextBox();
            Label lblDownload = new Label();
            Label lblPages = new Label();
            Label lblPublisher = new Label();
            LinkLabel lklDownload = new LinkLabel();
            Panel pnlImage = new Panel();

            #region expandCollapsePanel1

            expandCollapsePanel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            expandCollapsePanel1.ButtonSize = MakarovDev.ExpandCollapsePanel.ExpandCollapseButton.ExpandButtonSize.Normal;
            expandCollapsePanel1.ButtonStyle = MakarovDev.ExpandCollapsePanel.ExpandCollapseButton.ExpandButtonStyle.Circle;
            expandCollapsePanel1.Controls.Add(pnlImage);
            expandCollapsePanel1.Controls.Add(lklDownload);
            expandCollapsePanel1.Controls.Add(txtDescription);
            expandCollapsePanel1.Controls.Add(txtPublisher);
            expandCollapsePanel1.Controls.Add(txtPages);
            expandCollapsePanel1.Controls.Add(lblDownload);
            expandCollapsePanel1.Controls.Add(txtYear);
            expandCollapsePanel1.Controls.Add(lblPages);
            expandCollapsePanel1.Controls.Add(txtISBN);
            expandCollapsePanel1.Controls.Add(lblYear);
            expandCollapsePanel1.Controls.Add(lblDescription);
            expandCollapsePanel1.Controls.Add(lblISBN);
            expandCollapsePanel1.Controls.Add(txtID);
            expandCollapsePanel1.Controls.Add(lblID);
            expandCollapsePanel1.Controls.Add(txtTitle);
            expandCollapsePanel1.Controls.Add(txtSubtitle);
            expandCollapsePanel1.Controls.Add(lblTitle);
            expandCollapsePanel1.Controls.Add(lblSubtitle);
            expandCollapsePanel1.Controls.Add(txtAuthors);
            expandCollapsePanel1.Controls.Add(lblAuthors);
            expandCollapsePanel1.Controls.Add(lblPublisher);
            expandCollapsePanel1.Dock = System.Windows.Forms.DockStyle.Top;
            expandCollapsePanel1.ExpandedHeight = 0;
            expandCollapsePanel1.IsExpanded = true;
            expandCollapsePanel1.Location = new System.Drawing.Point(0, 0);
            expandCollapsePanel1.Name = "expandCollapsePanel" + ebook.ID;
            expandCollapsePanel1.Size = new System.Drawing.Size(673, 330);
            expandCollapsePanel1.TabIndex = 0;
            expandCollapsePanel1.Text = String.Format("Details: {0}", ebook.Title);
            expandCollapsePanel1.UseAnimation = false;
            //(expandCollapsePanel1.Controls[0].Controls[0].Parent as ExpandCollapseButton)


            #endregion

            #region pctBox

            pctBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            pctBox.Location = new System.Drawing.Point(3, 56);
            pctBox.Name = "pctBox";
            pctBox.Size = new System.Drawing.Size(197, 269);
            pctBox.TabIndex = 5;
            pctBox.TabStop = false;
            pctBox.Image = ebook.Image;

            #endregion

            #region txtDesctiption

            txtDescription.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            txtDescription.Location = new System.Drawing.Point(84, 185);
            txtDescription.Multiline = true;
            txtDescription.Name = "txtDesctiption";
            txtDescription.Size = new System.Drawing.Size(217, 65);
            txtDescription.TabIndex = 4;
            txtDescription.Text = ebook.Description;
            txtDescription.ReadOnly = true;

            #endregion

            #region txtISBN

            txtISBN.Location = new System.Drawing.Point(336, 56);
            txtISBN.Name = "txtISBN";
            txtISBN.Size = new System.Drawing.Size(132, 21);
            txtISBN.TabIndex = 4;
            txtISBN.Text = ebook.ISBN.ToString();
            txtISBN.ReadOnly = true;

            #endregion

            #region lblDescription

            lblDescription.AutoSize = true;
            lblDescription.Location = new System.Drawing.Point(11, 206);
            lblDescription.Name = "lblDescription";
            lblDescription.Size = new System.Drawing.Size(72, 15);
            lblDescription.TabIndex = 3;
            lblDescription.Text = "Description:";

            #endregion

            #region lblISBN

            lblISBN.AutoSize = true;
            lblISBN.Location = new System.Drawing.Point(275, 56);
            lblISBN.Name = "lblISBN";
            lblISBN.Size = new System.Drawing.Size(38, 15);
            lblISBN.TabIndex = 3;
            lblISBN.Text = "ISBN:";

            #endregion

            #region txtSubtitle

            txtSubtitle.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            txtSubtitle.Location = new System.Drawing.Point(336, 120);
            txtSubtitle.Name = "txtSubtitle";
            txtSubtitle.Size = new System.Drawing.Size(132, 21);
            txtSubtitle.TabIndex = 2;
            txtSubtitle.Text = ebook.SubTitle;
            txtSubtitle.ReadOnly = true;

            #endregion

            #region lblSubtitle

            lblSubtitle.AutoSize = true;
            lblSubtitle.Location = new System.Drawing.Point(275, 120);
            lblSubtitle.Name = "lblSubtitle";
            lblSubtitle.Size = new System.Drawing.Size(51, 15);
            lblSubtitle.TabIndex = 1;
            lblSubtitle.Text = "Subtitle:";

            #endregion

            #region txtAuthors

            txtAuthors.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            txtAuthors.Location = new System.Drawing.Point(336, 151);
            txtAuthors.Name = "txtAuthors";
            txtAuthors.Size = new System.Drawing.Size(132, 21);
            txtAuthors.TabIndex = 2;

            StringBuilder authors = new StringBuilder();

            for (int i = 0; i < ebook.Authors.Length; i++)
            {
                if (i + 1 == ebook.Authors.Length)
                {
                    authors.Append(string.Format("{0}", ebook.Authors[i]));
                }
                else
                {
                    authors.Append(string.Format("{0}, ", ebook.Authors[i]));
                }
            }
            //TODO: corrigir exibicao de autores
            txtAuthors.Text = authors.ToString();
            txtAuthors.ReadOnly = true;

            #endregion

            #region lblAuthors

            lblAuthors.AutoSize = true;
            lblAuthors.Location = new System.Drawing.Point(275, 151);
            lblAuthors.Name = "lblAuthors";
            lblAuthors.Size = new System.Drawing.Size(51, 15);
            lblAuthors.TabIndex = 1;
            lblAuthors.Text = "Author(s):";

            #endregion

            #region lblID

            lblID.AutoSize = true;
            lblID.Location = new System.Drawing.Point(23, 56);
            lblID.Name = "lblID";
            lblID.Size = new System.Drawing.Size(22, 15);
            lblID.TabIndex = 1;
            lblID.Text = "ID:";

            #endregion

            #region txtID

            txtID.Location = new System.Drawing.Point(84, 56);
            txtID.Name = "txtID";
            txtID.Size = new System.Drawing.Size(159, 21);
            txtID.TabIndex = 2;
            txtID.Text = ebook.ID.ToString();
            txtID.ReadOnly = true;

            #endregion

            #region lblTitle

            lblTitle.AutoSize = true;
            lblTitle.Location = new System.Drawing.Point(275, 87);
            lblTitle.Name = "lblTitle";
            lblTitle.Size = new System.Drawing.Size(33, 15);
            lblTitle.TabIndex = 1;
            lblTitle.Text = "Title:";

            #endregion

            #region txtTitle

            txtTitle.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            txtTitle.Location = new System.Drawing.Point(336, 87);
            txtTitle.Name = "txtTitle";
            txtTitle.Size = new System.Drawing.Size(132, 21);
            txtTitle.TabIndex = 2;
            txtTitle.Text = ebook.Title;
            txtTitle.ReadOnly = true;

            #endregion

            #region lblYear

            lblYear.AutoSize = true;
            lblYear.Location = new System.Drawing.Point(23, 87);
            lblYear.Name = "lblYear";
            lblYear.Size = new System.Drawing.Size(35, 15);
            lblYear.TabIndex = 3;
            lblYear.Text = "Year:";

            #endregion

            #region txtYear

            txtYear.Location = new System.Drawing.Point(84, 84);
            txtYear.Name = "txtYear";
            txtYear.Size = new System.Drawing.Size(55, 21);
            txtYear.TabIndex = 4;
            txtYear.Text = ebook.Year.ToString();
            txtYear.ReadOnly = true;

            #endregion

            #region lblPages

            lblPages.AutoSize = true;
            lblPages.Location = new System.Drawing.Point(23, 119);
            lblPages.Name = "lblPages";
            lblPages.Size = new System.Drawing.Size(45, 15);
            lblPages.TabIndex = 3;
            lblPages.Text = "Pages:";

            #endregion

            #region txtPages

            txtPages.Location = new System.Drawing.Point(84, 117);
            txtPages.Name = "txtPages";
            txtPages.Size = new System.Drawing.Size(55, 21);
            txtPages.TabIndex = 4;
            txtPages.Text = ebook.Pages.ToString();
            txtPages.ReadOnly = true;

            #endregion

            #region lblDownload

            lblDownload.AutoSize = true;
            lblDownload.Location = new System.Drawing.Point(9, 258);
            lblDownload.Name = "lblDownload";
            lblDownload.Size = new System.Drawing.Size(66, 15);
            lblDownload.TabIndex = 3;
            lblDownload.Text = "Download:";

            #endregion

            #region txtPublisher

            txtPublisher.Location = new System.Drawing.Point(84, 148);
            txtPublisher.Name = "txtPublisher";
            txtPublisher.Size = new System.Drawing.Size(171, 21);
            txtPublisher.TabIndex = 4;
            txtPublisher.Text = ebook.Publisher;
            txtPublisher.ReadOnly = true;
            #endregion

            #region lblPublisher

            lblPublisher.AutoSize = true;
            lblPublisher.Location = new System.Drawing.Point(23, 151);
            lblPublisher.Name = "lblPublisher";
            lblPublisher.Size = new System.Drawing.Size(62, 15);
            lblPublisher.TabIndex = 3;
            lblPublisher.Text = "Publisher:";

            #endregion

            #region lklDownload

            lklDownload.AutoSize = true;
            lklDownload.Location = new System.Drawing.Point(81, 258);
            lklDownload.Name = "lklDownload";
            lklDownload.Size = new System.Drawing.Size(26, 15);
            lklDownload.TabIndex = 6;
            lklDownload.TabStop = true;
            lklDownload.Text = ebook.Link;
            lklDownload.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(delegate(object sender, LinkLabelLinkClickedEventArgs e)
            {
                System.Diagnostics.Process.Start(ebook.Link);
            });

            #endregion

            #region pnlImage

            pnlImage.Controls.Add(pctBox);
            pnlImage.Dock = System.Windows.Forms.DockStyle.Right;
            pnlImage.Location = new System.Drawing.Point(471, 0);
            pnlImage.Name = "pnlImage";
            pnlImage.Size = new System.Drawing.Size(200, 328);
            pnlImage.TabIndex = 7;

            #endregion

            expandCollapsePanel1.ResumeLayout(false);
            expandCollapsePanel1.PerformLayout();
            return expandCollapsePanel1;
        }

        private void showDetailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowEbookDetails(this.dataGridView1.SelectedRows);
        }

        private void UpdatePager(QueryResult result)
        {
            if (this.actualPage < (result.Total / 10) && this.actualPage == 1)
            {
                this.btnLast.Enabled = true;
                this.btnPrevious.Enabled = false;
                this.btnNext.Enabled = true;
                this.btnFirst.Enabled = false;
            }
            else if (this.actualPage < (result.Total / 10) && this.actualPage > 1)
            {
                this.btnLast.Enabled = true;
                this.btnPrevious.Enabled = true;
                this.btnNext.Enabled = true;
                this.btnFirst.Enabled = true;
            }
            else if (this.actualPage == (result.Total / 10))
            {
                this.btnLast.Enabled = false;
                this.btnNext.Enabled = false;
                this.btnPrevious.Enabled = true;
                this.btnFirst.Enabled = true;
            }
            else if (this.actualPage == 1)
            {
                this.btnFirst.Enabled = true;
                this.btnPrevious.Enabled = true;

                this.btnNext.Enabled = true;
                this.btnLast.Enabled = true;
            }

            this.txtPage.Text = String.Format("{0}/{1}", result.Page, result.Total / 10);
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            this.actualPage++;
            this.query.Page = this.actualPage;
            this.Find(this.query);

        }

        private void Find(EBookQuery query)
        {
            JSONEBookConverter converter = new JSONEBookConverter(query);

            queryResult = converter.Find();

            this.UpdatePager(queryResult);
            this.dataGridView1.DataSource = queryResult.EBooks;

        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            this.actualPage--;
            this.query.Page = this.actualPage;
            this.Find(this.query);
        }

        private void btnFirst_Click(object sender, EventArgs e)
        {
            this.actualPage = 1;
            this.query.Page = this.actualPage;
            this.Find(this.query);
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            this.actualPage = (this.queryResult.Total / 10);
            this.query.Page = this.actualPage;
            this.Find(this.query);
        }

        private void txtQuery_KeyUp(object sender, KeyEventArgs e)
        {
            int qtdChar = 50 - this.txtQuery.TextLength;
            if (this.txtQuery.TextLength == 0)
            {
                this.btnExecute.Enabled = false;
            }
            else
            {
                this.btnExecute.Enabled = true;
            }
            if (qtdChar <= 10)
            {
                this.stLabelChar.ForeColor = Color.Red;
            }
            else
            {
                this.stLabelChar.ForeColor = Color.Black;
            }

            this.stLabelChar.Text = String.Format("{0} charachter(s) left", qtdChar);
        }
    }
}
